<template>
  <div class="login-page">
  <el-card class="el-card">
  <template #header>黑马面经运营后台</template>
<el-form :model="form" :rules="rules" ref="loginForm">
  <el-form-item prop="username" label="用户名">
    <el-input placeholder="请输入用户名" v-model="form.username"></el-input>
  </el-form-item>
  <el-form-item  prop="password" label="密码">
    <el-input placeholder="请输入密码" v-model="form.password"></el-input>
  </el-form-item>
   <el-form-item>
    <el-button type="primary" @click="login">提交</el-button>
    <el-button @click="reset">重置</el-button>
  </el-form-item>
  </el-form>

</el-card>
  </div>
</template>

<script>
import { login } from '@/api/user'

export default {
  name: 'HmH5PcStudyIndex',

  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          // 长度必须在5-10个字符
          { min: 5, max: 10, message: '长度必须在5-10个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 10, message: '长度必须在5-10个字符', trigger: 'blur' }

        ]
      }

    }
  },

  methods: {
    login () {
      this.$refs.loginForm.validate(async valid => {
        // console.log(valid)
        // 调用登录ajax请求
        const res = await login(this.form)
        // console.log(res)
        // 将token 存到state
        this.$store.commit('user/setUserToken', res.data.token)

        // 跳转到首页
        this.$router.push('/')
        // 提示用户登录成功
        this.$message.success('登录成功')
      })
    },
    // 重置功能
    reset () {
      this.$refs.loginForm.resetFields()
    }

  }
}
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 420px;
    ::v-deep .el-card__header{
      height: 80px;
      background: rgba(114,124,245,1);
      text-align: center;
      line-height: 40px;
      color: #fff;
      font-size: 18px;
    }
  }
}

</style>
